package com.tzxs.pay.main.login;

import android.app.Application;
import android.arch.lifecycle.AndroidViewModel;
import android.databinding.ObservableArrayList;
import android.databinding.ObservableField;
import android.support.annotation.NonNull;
import android.util.Log;

import com.tzxs.pay.main.first.model.MsgNoticeBean;
import com.tzxs.pay.main.first.model.PendingOrderBean;
import com.tzxs.pay.main.login.model.LoginBean;
import com.tzxs.pay.main.login.model.UserDataResource;
import com.tzxs.pay.main.mine.model.MessageBean;
import com.tzxs.pay.main.mine.model.PasswordBean;
import com.tzxs.pay.util.ISharedPreference;
import com.tzxs.pay.util.SingleLiveEvent;
import com.tzxs.pay.util.netnavigator.SimpleNetNavigator;
import com.tzxs.pay.util.viewmodel.Callback;
import com.tzxs.pay.util.websocket.model.WebSocketBean;

import okhttp3.ResponseBody;
import retrofit2.Response;

public class LoginViewModel extends AndroidViewModel {
    private static final String TAG = LoginViewModel.class.getSimpleName();
    //给你写个例子
    //创建一个Snackbar或者Toast的LiveEvent
    public SingleLiveEvent<String> snackbarEvent = new SingleLiveEvent<>();
    public SingleLiveEvent<String> dialogEvent = new SingleLiveEvent<>();

    //短信验证码
    public ObservableField<MessageBean.DataBean> messageBean = new ObservableField<>();

    //是否显示密码
    public ObservableField<Boolean> isShowPassword = new ObservableField<>(false);


    //通知列表
    public ObservableArrayList<MsgNoticeBean.DataBean> noticeList = new ObservableArrayList<>();
    //即将过期订单列表
    public ObservableArrayList<PendingOrderBean.DataBean> pendingOrder = new ObservableArrayList<>();


    public LoginViewModel(@NonNull Application application) {
        super(application);


    }

    //登录
    public void login(String phone, String password, final Callback callback) {

        //登录成功
        UserDataResource.getInstance(getApplication()).login(phone, password,
                new SimpleNetNavigator<LoginBean>(snackbarEvent, dialogEvent, callback) {

                    @Override
                    public String getTitle() {

                        return "正在登录...";
                    }

                    @Override
                    public void success(LoginBean data) {
                        if (data.getToken() != null) {
                            callback.success();
                        } else {
                            snackbarEvent.setValue(data.getMsg());
                        }

                    }

                    @Override
                    public void fail(String error) {

                        Log.d(TAG, "fail: error" + error);
                        snackbarEvent.setValue(error);
                    }


                });

    }


    /**
     * 获取验证码
     *
     * @param phone    手机号
     * @param callback 回调
     */
    public void getVerCode(String phone, Callback callback) {
        UserDataResource.getInstance(getApplication()).getVerCode(phone, new SimpleNetNavigator<MessageBean>(dialogEvent, snackbarEvent, callback) {
            @Override
            public String getTitle() {
                return "正在获取验证码...";
            }

            @Override
            public void success(MessageBean data) {
                if (data.getCode().equals("10001")) {
                    messageBean.set(data.getData().get(0));
                    callback.success();
                } else {
                    snackbarEvent.setValue(data.getMsg());
                }
            }

            @Override
            public void fail(String error) {
                snackbarEvent.setValue(error);
            }
        });

    }

    /**
     * 修改支付密码
     *
     * @param newPassword  新密码
     * @param surePassword 确认密码
     * @param phone        手机号
     * @param code         验证码
     * @param callback     回调
     */
    public void changePassword(String newPassword, String surePassword, String phone, String code, Callback callback) {
        UserDataResource.getInstance(getApplication()).changePassword(newPassword, surePassword, phone, code,
                new SimpleNetNavigator<PasswordBean>(dialogEvent, snackbarEvent, callback) {
                    @Override
                    public String getTitle() {
                        return "正在修改密码...";
                    }

                    @Override
                    public void success(PasswordBean data) {
                        if (data.getCode().equals("10001")) {
                            callback.success();
                            snackbarEvent.setValue(data.getMsg());
                        } else {
                            snackbarEvent.setValue(data.getMsg() + ":" + data.getCode());
                        }

                    }

                    @Override
                    public void fail(String error) {
                        snackbarEvent.setValue(error);
                    }
                });
    }


    //获取通知列表
    public void getNoticeList(int offset, int limmit, Callback callback) {

        UserDataResource.getInstance(getApplication()).getNoticeList(offset, limmit, new SimpleNetNavigator<MsgNoticeBean>(snackbarEvent, dialogEvent, callback) {
            @Override
            public String getTitle() {
                return null;
            }

            @Override
            public void success(MsgNoticeBean data) {

                if (data.getCode().equals("10001")) {
                    noticeList.clear();
                    //只添加第一条
                    noticeList.add(data.getData().get(0));
                     if (noticeList.size()>0) {
                         callback.success();
                     }

                } else {
                    snackbarEvent.setValue(data.getMsg());
                }
            }

            @Override
            public void fail(String error) {
                snackbarEvent.setValue(error);
            }
        });
    }

    //获取通知详情列表
    public void getNoticeDetail(int offset, int limmit, Callback callback) {

        UserDataResource.getInstance(getApplication()).noticeDetail(offset, limmit, new SimpleNetNavigator<PendingOrderBean>(snackbarEvent, dialogEvent, callback) {
            @Override
            public String getTitle() {
                return null;
            }

            @Override
            public void success(PendingOrderBean data) {
                 Log.i("2333",data.getData().size()+"size");
                if (data.getCode().equals("10001")) {

                    if (offset == 1) {
                        pendingOrder.clear();
                        pendingOrder.addAll(data.getData());
                    } else {
                        pendingOrder.addAll(data.getData());
                    }

                } else {
                    snackbarEvent.setValue(data.getMsg());
                }
            }

            @Override
            public void fail(String error) {
                snackbarEvent.setValue(error);
            }
        });
    }

}
